package br.com.lh.urabuilder.util
{
	import br.com.lh.urabuilder.model.UraModelLocator;
	
	import flash.geom.Point;
	
	import mx.core.UIComponent;

	public class CoordinatesUtils
	{

		public static const SELF_ARROW_WIDTH:int=30;
		public static const SELF_ARROW_HEIGHT:int=60;

		public function CoordinatesUtils(){
		}
		
		public static function getPointConvertedIntoCanvasScale(point:Point):Point{
			var canvasPoint:Point = getCanvasLocalToGlobalCoordinates();
			return new Point(point.x - canvasPoint.x, point.y - canvasPoint.y);
		}
		

		public static function getCanvasLocalToGlobalCoordinates():Point
		{
			var componentCanvas:UIComponent=UraModelLocator.getInstance().parent;
			var point:Point = componentCanvas.contentToGlobal(new Point(componentCanvas.x, componentCanvas.y));
			return new Point(point.x - componentCanvas.x,point.y - componentCanvas.y);
		}

		public static function getLineLength(startPoint:Point, endPoint:Point):Number
		{
			var deltaX:Number=endPoint.x - startPoint.x;
			var deltaY:Number=endPoint.y - startPoint.y;
			return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
		}

		public static function getAngleInRad(startPoint:Point, endPoint:Point):Number
		{
			var deltaX:Number=endPoint.x - startPoint.x;
			var deltaY:Number=endPoint.y - startPoint.y;
			if (deltaX ==0 && deltaY ==0){
				return 0;
			}
			
			return Math.atan(deltaY / deltaX);
		}

		public static function getAngleInDegrees(startPoint:Point, endPoint:Point):Number
		{
			var deltaX:Number=endPoint.x - startPoint.x;
			var deltaY:Number=endPoint.y - startPoint.y;
			if (deltaX == 0)
			{
				return deltaY == 0 ? 0 : deltaY > 0 ? 90 : 270;
			}


			var baseValue:Number=Math.atan(deltaY / deltaX) * 180 / Math.PI
			if (deltaY >= 0 && deltaX >= 0)
			{
				return baseValue;
			}
			else if (deltaY > 0 && deltaX < 0)
			{
				return 180 + baseValue;
			}
			else if (deltaY < 0 && deltaX > 0)
			{
				return 360 + baseValue;
			}
			else
			{
				return 180 + baseValue;
			}
		}

		public static function getRotatedStartPoint(startPoint:Point, height:Number, angle:Number):Point
		{
			return new Point(startPoint.x + (height / 2 * Math.cos(angle)), startPoint.y - (height / 2 * Math.sin(angle)));
		}


	}
}


